package com.skiving.cloud.common.utils;

import java.io.Serializable;

/**
 * 做一个通用的返回值类型
 * @author hushouquan
 */
public class ReturnT<T> implements Serializable{
    public static final int SUCCESS_CODE = 0;
    public static final int FAIL_CODE = 500;
    
    private int code;
    private String message;
    private T data;
    
    public ReturnT(int code, String message, T data){
        this.code = code;
        this.message = message;
        this.data = data;
    }
    
    public ReturnT(int code, String message){
        this.code = code;
        this.message = message;
    }
    
    public ReturnT(){
        super();
    }
    
    public int getCode(){
        return code;
    }
    
    public void setCode(int code){
        this.code = code;
    }
    
    public String getMessage(){
        return message;
    }
    
    public void setMessage(String message){
        this.message = message;
    }
    
    public T getData(){
        return data;
    }
    
    public void setData(T data){
        this.data = data;
    }
    
    @Override
    public String toString(){
        return "ReturnT{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
    
    public static <T> ReturnT<T> error(){
        return error("未知异常，请联系管理员");
    }
    
    public static <T> ReturnT<T> error(String msg){
        return error(FAIL_CODE, msg);
    }
    public static <T> ReturnT<T> error(String msg, T data) {
        return new ReturnT<>(FAIL_CODE, msg, data);
    }

    public static <T> ReturnT<T> error(int code, String msg) {
        return new ReturnT<>(code, msg);
    }
    
    public static <T> ReturnT<T> error(int code, String msg, T data) {
        return new ReturnT<>(code, msg, data);
    }
    
    public static <T> ReturnT<T> ok(){
        return new ReturnT<>(SUCCESS_CODE, "成功");
    }
    
    public static <T> ReturnT<T> ok(T data) {
        return new ReturnT<>(SUCCESS_CODE, "成功", data);
    }

    public static <T> ReturnT<T> ok(String message, T data) {
        return new ReturnT<>(SUCCESS_CODE, message, data);
    }
}
